// batchcrop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlbase.h>
#include <math.h>
#include <gdiplus.h>

using namespace Gdiplus;
CLSID encoderClsid;

Gdiplus::Bitmap* bm;
int width;
int height; 

bool isColumnClear(int column)
{

	Gdiplus::Color color;
	for (int y = 0; y < height; y++)

	{
		bm->GetPixel(column, y, &color);
		if (color.GetR()!=0 ) return false;
		if (color.GetG()!=0 ) return false;	
		if (color.GetB()!=0 ) return false;	

	}

	return true;	
}



bool isRowClear(int row)
{

	Gdiplus::Color color;
	for (int x = 0; x < width; x++)

	{
		bm->GetPixel(x, row, &color);
		if (color.GetR()!=0 ) return false;
		if (color.GetG()!=0 ) return false;	
		if (color.GetB()!=0 ) return false;	

	}

	return true;	
}

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);

void crop(char* infile, char* outfile, char* offsetfile)
{
	USES_CONVERSION;
	bm = new Gdiplus::Bitmap(T2W(infile));


	width = bm->GetWidth();
	height = bm->GetHeight();  
	Gdiplus::Color color;

	int x;
	for (x = 0; x < width; x++)
		if (!isColumnClear(x)) break;


	int y;
	for (y = 0; y < height; y++)
		if (!isRowClear(y)) break;


	int endx;
	for (endx = width-1; endx > 0 ; endx--)
		if (!isColumnClear(endx)) break;


	int endy;
	for (endy = height-1; endy > 0 ; endy--)
		if (!isRowClear(endy)) break;


	int newwidth=endx-x;
	int newheight=endy-y;

	// empty image - no sprite found
	if (newwidth <= 0 || newheight <= 0)
	{
		Gdiplus::Bitmap newbm(1, 1);
		newbm.SetPixel(0, 0, Gdiplus::Color(0,0,0,0));
		newbm.Save(T2W(outfile), &encoderClsid);

		FILE* stream;

		stream  = fopen( offsetfile, "w" );
		fprintf(stream,"%d,%d\n", 0, 0);
		fprintf(stream,"%d,%d\n", 1, 1);
		fclose( stream );

		delete bm;
		return;
	}



	Gdiplus::Bitmap newbm(newwidth, newheight);


	for (int yctr = 0; yctr < newheight; yctr++)
		for (int xctr = 0; xctr < newwidth; xctr++)
		{
			bm->GetPixel(xctr+x, yctr+y, &color);
			newbm.SetPixel(xctr, yctr, color);
		}




		//USES_CONVERSION;
		newbm.Save(T2W(outfile), &encoderClsid);

		FILE* stream;

		stream  = fopen( offsetfile, "w" );
		fprintf(stream,"%d,%d\n", x, y);
		fprintf(stream,"%d,%d\n", newwidth, newheight);
		fclose( stream );

		delete bm;

}




int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;
	GetImageEncodersSize(&num, &size);
	if(size == 0) return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL) return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);
	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}    
	}
	free(pImageCodecInfo);
	return -1;  // Failure
}



int _tmain(int argc, _TCHAR* argv[])
{


	ULONG_PTR gdiplusToken;



	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	int res=Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


		


		/*		if (ofn.nFilterIndex == 1) GetEncoderClsid(L"image/bmp", &encoderClsid);
		else if (ofn.nFilterIndex == 2) GetEncoderClsid(L"image/gif", &encoderClsid);
		else if (ofn.nFilterIndex == 3) GetEncoderClsid(L"image/jpeg", &encoderClsid);
		else if (ofn.nFilterIndex == 4) GetEncoderClsid(L"image/png", &encoderClsid);
		else if (ofn.nFilterIndex == 5) GetEncoderClsid(L"image/tiff", &encoderClsid);*/

		//GetEncoderClsid(L"image/jpeg", &encoderClsid);
		GetEncoderClsid(L"image/png", &encoderClsid);



	char infilename[256];
	char outfilename[256];
	char offsetfilename[256];

	for (int ctr=0; ctr<1820; ctr++)
	{

		sprintf(infilename, "C:\\sfmltest\\actions\\act%04d.png", ctr);
		sprintf(outfilename, "C:\\sfmltest\\cropped2\\act%04d.png", ctr);
		sprintf(offsetfilename, "C:\\sfmltest\\cropped2\\act%04d.offset", ctr);

		printf("%s -> %s\n", infilename, outfilename);
		crop(infilename,outfilename,offsetfilename);

	


	}


	
	Gdiplus::GdiplusShutdown(gdiplusToken);
	return 0;
}

